package sgfapp.uno;

//import android.R;
import java.io.IOException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Locale;

import org.json.JSONObject;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.support.v4.app.FragmentActivity;
import android.support.v7.*;
import android.app.Activity;
import android.app.ProgressDialog;

import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;


import com.facebook.*;
import com.facebook.model.*;
import com.facebook.widget.LoginButton;

import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import sgfapp.entities.BdHelper;
import sgfapp.entities.DataBaseManager;
import sgfapp.entities.Usuario;

public class MainActivity extends FragmentActivity implements OnClickListener{
	
	
	
	
	

	final String TAG = "GCMDemo";
	private WebView webView;   	
    Button btn;
    private MainFragment mainFragment;
    private ProgressDialog pDialog;
    
	@Override
    protected void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        


        
        
        if (savedInstanceState == null) {
        	setContentView(R.layout.activity_main);
        	LoginButton authButton = (LoginButton) this.findViewById(R.id.login_button);
        	authButton.setReadPermissions(Arrays.asList("public_profile","email"));
 
         //DataBaseManager manager = new DataBaseManager(this);
	    
	       
	      //  btn=(Button) findViewById(R.id.button1);
	        
	
	        Log.e(TAG,"Sin session entra a activarla");
	        
	        /*
	        btn.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	               Log.e(TAG,"LLAMO ACTIVITY");
	            	Intent intent = new Intent(MainActivity.this, activity_menu.class);
	               startActivity(intent);
	            }
	         });
	       */
	        
	        
	        mainFragment = new MainFragment();
	        getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
	        

        }
        else
        {
            mainFragment = (MainFragment) getSupportFragmentManager()
                    .findFragmentById(android.R.id.content);
        }
        
    }

	
	
	//facebook
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
	      Log.e("GCMDemo","Data: "+data.toString());
	      Log.e("GCMDemo","requestCode: "+Integer.toString(requestCode));
	      Log.e("GCMDemo","resultCode: "+Integer.toString(resultCode));
	    if(resultCode!=0)
	    {
	    Usuario.CargarUsuario(this,pDialog);
        Intent intent = new Intent(MainActivity.this, activity_menu.class);
        startActivity(intent);
        finish();
	    }
	    else
	    {
	    	Context context = getApplicationContext();
	    	CharSequence text = "No se ha podido iniciar sesion en facebook por favor intenta nuevamente.";
	    	int duration = Toast.LENGTH_SHORT;

	    	Toast toast = Toast.makeText(context, text, duration);
	    	toast.show();
	    }
	    
        
    } 
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.logout:

		    	Intent intent = new Intent(this, MainActivity.class);
		        startActivity(intent);
		        finish();
	        case R.id.action_info:

		    	Intent intent2 = new Intent(this, Info.class);
		        startActivity(intent2);
		        
	    }
	    return true;
	}

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public void onClick(View v) {
       /* switch (v.getId()) {
        case R.id.button1:
        	Log.e(TAG,"LLAMO ACTIVITY");
        	Intent intent = new Intent(MainActivity.this, activity_menu.class);
            startActivity(intent);

            break;
        }*/
    }
   private void PrintHASH()
   {
	   try
	   {
           PackageInfo info = getPackageManager().getPackageInfo("sgfapp.uno", PackageManager.GET_SIGNATURES);
           for (Signature signature : info.signatures) 
           {
               MessageDigest md = MessageDigest.getInstance("SHA");
               md.update(signature.toByteArray());
               Log.e("TAG:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
           }
	   } catch (NameNotFoundException e) 
	   {
	   } catch (NoSuchAlgorithmException e) {
	   }
	   
   }

}
